<template>
    <div class="addBrand-container">
      <el-page-header @back="goBack" :content="content" style="margin-bottom:20px">
      </el-page-header>
    <div class="container">
      <el-form ref="ruleForm"
               :model="ruleForm"
               :rules="Rules"
               :inline="false"
               label-width="180px"
               class="demo-ruleForm">
        <el-form-item label="头像:"
                      prop="avatar">
          <uploadImage class="uploadImage" :imageUrl="ruleForm.avatar" @imageUrl2="imageUrl2" />
        </el-form-item>
        <el-form-item label="账号:"
                      prop="username">
          <el-input v-model="ruleForm.username"
                    placeholder="请输入账号"
                    maxlength="20" />
        </el-form-item>
        <el-form-item label="员工姓名:"
                      prop="name">
          <el-input v-model="ruleForm.name"
                    placeholder="请输入员工姓名"
                    maxlength="12" />
        </el-form-item>
        <el-form-item label="手机号:"
                      prop="phone">
          <el-input v-model="ruleForm.phone"
                    placeholder="请输入手机号"
                    maxlength="11" />
        </el-form-item>
        <el-form-item label="性别:"
                      prop="sex">
          <el-radio-group v-model="ruleForm.sex">
            <el-radio :label="0" >男</el-radio>
            <el-radio :label="1" >女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="职位:"
                      prop="role">
          <el-radio-group v-model="ruleForm.roleId">
            <el-radio :label="2" >总经理</el-radio>
            <el-radio :label="3" >店主</el-radio>
            <el-radio :label="4" >店员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属商店">
          <el-select v-model="ruleForm.storeId" placeholder="商店" >
            <el-option :label="store.name" :value="store.id" v-for="(store,index) in stores" :key="index"></el-option>
            <!-- <el-option label="区域二" value="beijing"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号:"
                      prop="idNumber"
                      class="idNumber">
          <el-input v-model="ruleForm.idNumber"
                    placeholder="请输入身份证号"
                    maxlength="18" />
        </el-form-item>
        <div class="subBox address">
          <!-- <el-form-item> -->
          <el-button @click="() => $router.push('/home/user')">
            取消
          </el-button>
          <el-button type="primary"
                     :class="{ continue: actionType === 'add' }"
                     @click="submitForm(actionType)">
            保存
          </el-button>
          <!-- <el-button v-if="actionType == 'add'"
                     type="primary"
                     @click="submitForm('ruleForm', true)">
            保存并继续添加
          </el-button> -->
          <!-- </el-form-item> -->
        </div>
      </el-form>
    </div>
  </div>
</template>


<script>
import uploadImage from '@/components/upload/uploadImage.vue'
import {addUser,queryById,updateUser} from '@/api/user'
import {storePage} from '@/api/store'
export default{
  components:{
    uploadImage
  },
  data() {
    var checkPhone = (rule, value, callback) => { // 手机号验证
            if (!value) {
              return callback(new Error('手机号不能为空'));
            } else {
              const reg = /^1[3456789]\d{9}$/
              if (reg.test(value)) {
                callback();
              } else {
                return callback(new Error('请输入正确的手机号'));
              }
            }
          };
          var checkIdNumber = (rule, value, callback) => { // 手机号验证
            if (!value) {
              return callback(new Error('身份证号不能为空'));
            } else {
              const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
              if (reg.test(value)) {
                callback();
              } else {
                return callback(new Error('请输入正确的身份证号'));
              }
            }
          };
    return {
      Rules:{
              phone: [
                { required: true, message: "请输入正确手机号", trigger: "blur" },
                {validator: checkPhone, trigger: 'blur'}
              ],
              idNumber:[
                { required: true, message: "请输入正确身份证号", trigger: "blur" },
                {validator: checkIdNumber, trigger: 'blur'}
              ],
            },
      content:'添加员工',
      ruleForm:{
        name: '',
        phone: '',
        sex: 0,
        idNumber: '',
        username: '',
        roleId:3,
        avatar:'',
        storeId:1,
        id:''
      },
      rules:'',
      actionType:'',
      stores:[],
      user:null
    }
  },
  created(){
    let res;
    this.actionType = this.$route.query.methods
    if(this.actionType == 'add'){
      this.content='添加员工'
    }else{
      this.content='修改员工信息'
      res = queryById(this.$route.query.id)
      res.then((res)=>{
        // this.ruleForm =  Object.assign(res.data)
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = res.data[key] == undefined ? this.ruleForm[key] : res.data[key]
      })
      }).catch((err)=>{
        this.$message.error(err)
      })
      
    }
    //查询所有store
    const res2 = storePage('',1,10)
    res2.then((res)=>{
      console.log(res.data)
      this.stores = res.data.records
    }).catch((err)=>{
      this.$message.error(err)
    })
    
  },
  methods:{
    submitForm(value){
      console.log(this.ruleForm)
      let res;
      if(value == 'add'){
        res = addUser(this.ruleForm)
        res.then((res)=>{
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.$router.push('/home/user')
        }).catch((err)=>{
          this.$message.error(err);
        })
      }
      else if(value == 'update'){
        console.log(value)
        console.log(this.ruleForm)
        res = updateUser(this.ruleForm)
        res.then((res)=>{
          this.$message({
            message: '更新成功',
            type: 'success'
          });
          console.log(res)
          this.$router.push('/home/user')
        }).catch((err)=>{
          console.log(err)
          this.$message.error(err);
        })
      }
    },
    goBack(){
      this.$router.go(-1)
    },
    imageUrl2(imageUrl2){
          console.log('imageUrl2:'+imageUrl2)
          this.ruleForm.avatar = imageUrl2
    }
  }
}
</script>

<style lang="scss" scoped>
.addBrand {
  &-container {
    margin: 30px;
    margin-top: 0px;
    .HeadLable {
      background-color: transparent;
      margin-bottom: 0px;
      padding-left: 0px;
    }
    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 4px;

    }
    .idNumber {
      margin-bottom: 39px;
    }

    .el-form-item {
      margin-bottom: 29px;
    }
    .el-input {
      width: 293px;
    }
  }
}

.demo-ruleForm{
  width: 600px;
}

.uploadImage{
  border-radius: 5px;
}
</style>